<?php

namespace App\Admin\Forms;

use App\Models\Config;
use Dcat\Admin\Widgets\Form;

class Setting extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        foreach ($input as $title => $value) {
            $configModel = new Config();
            if ($title == 'copyright') {
                $value = clean($value, 'jiuge_config');
            }
            $configModel->createOrUpdate($title, $value);
        }

        return $this
            ->response()
            ->success('更新成功')
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('title', '网站标题')
            ->required();
        $this->text('keyword', '网站关键词')
            ->required();
        $this->text('description', '网站描述')
            ->required();
        $this->image('logo', '网站LOGO')
            ->autoUpload()
            ->move('/system/' . date('Ymd'))
            ->uniqueName()
            ->required();
        $this->textarea('copyright', '版权')
            ->required();
        $this->text('qq', '客服QQ')
            ->required();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        $configModel = new Config();
        return [
            'title'       => $configModel->getConfig('title'),
            'keyword'     => $configModel->getConfig('keyword'),
            'description' => $configModel->getConfig('description'),
            'logo'        => $configModel->getConfig('logo'),
            'copyright'   => $configModel->getConfig('copyright'),
            'qq'          => $configModel->getConfig('qq'),
        ];
    }
}
